package GUILayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JSeparator;

import CtrlLayer.ItemCtrl;
import CtrlLayer.OrderCtrl;
import CtrlLayer.PersonCtrl;

public class GUIMain
{
	private PersonCtrl PC;
	private ItemCtrl IC;
	private OrderCtrl OC;
	
	private JFrame frmVestbjergByggecenterAs;
	private JPanel panelKunde;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUIMain window = new GUIMain();
					window.frmVestbjergByggecenterAs.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public GUIMain() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		PC = new PersonCtrl();
		IC = new ItemCtrl();
		OC = new OrderCtrl();
		loadFromFile();
		frmVestbjergByggecenterAs = new JFrame();
		frmVestbjergByggecenterAs.setTitle("Vestbjerg Byggecenter A/S");
		frmVestbjergByggecenterAs.setBounds(100, 100, 960, 582);
		frmVestbjergByggecenterAs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		makeTopMenuBar();
		
		JPanel panelTapPan = new JPanel();
		frmVestbjergByggecenterAs.getContentPane().add(panelTapPan, BorderLayout.CENTER);
		panelTapPan.setLayout(new BorderLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
		panelTapPan.add(tabbedPane);
		
		JPanel panelLogin = new PanelLogin();
		tabbedPane.addTab("Login", null, panelLogin, null);
		
		JPanel panelSalg = new PanelSalg();
		tabbedPane.addTab("Salg", null, panelSalg, null);
		
		JPanel panelUdlejning = new PanelUdlejning();
		tabbedPane.addTab("Udlejning", null, panelUdlejning, null);
		
		panelKunde = new PanelKunde();
		tabbedPane.addTab("Kunde", null, panelKunde, null);
		
		JPanel panelOrdre = new PanelOrdre();
		tabbedPane.addTab("Ordre", null, panelOrdre, null);
		
		JPanel panelIndkomst = new PanelIndkomst();
		tabbedPane.addTab("Indkomst", null, panelIndkomst, null);
		
		JPanel panelLeverandoer = new PanelLeverandoer();
		tabbedPane.addTab("Leverand\u00F8r", null, panelLeverandoer, null);
		
		JPanel panelLager = new PanelLager();
		tabbedPane.addTab("Lager", null, panelLager, null);
		
		JPanel panelMedarbejder = new PanelMedarbejder();
		tabbedPane.addTab("Medarbejder", null, panelMedarbejder, null);
		
		JPanel panelLoen = new PanelLoen();
		tabbedPane.addTab("L\u00F8n", null, panelLoen, null);
		
		JPanel panelKartotek = new PanelKartotek();
		tabbedPane.addTab("Kartotek", null, panelKartotek, null);
	}
	
	private void makeTopMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		frmVestbjergByggecenterAs.setJMenuBar(menuBar);
		
		JMenu mnFiler = new JMenu("Filer");
		menuBar.add(mnFiler);
		
		JMenuItem mntmIndlaesData = new JMenuItem("Indlaes data");
		mntmIndlaesData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadFromFile();
			}
		});
		mnFiler.add(mntmIndlaesData);
		
		JMenuItem mntmGaemData = new JMenuItem("Gaem data");
		mntmGaemData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveToFile();
			}
		});
		mnFiler.add(mntmGaemData);
		
		JSeparator separator = new JSeparator();
		mnFiler.add(separator);
		
		JMenuItem mntmPrint = new JMenuItem("Print");
		mnFiler.add(mntmPrint);
		
		JMenuItem mntmAfslut = new JMenuItem("Afslut");
		mntmAfslut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				saveToFile();
				System.exit(0);
			}
		});
		
		JSeparator separator_1 = new JSeparator();
		mnFiler.add(separator_1);
		mnFiler.add(mntmAfslut);
		
		JMenu mnNewMenu = new JMenu("Rediger");
		menuBar.add(mnNewMenu);
		
		JMenu mnVis = new JMenu("Vis");
		menuBar.add(mnVis);
		
		JMenu mnLogUd = new JMenu("Log Ud");
		menuBar.add(mnLogUd);
		
		JMenu mnHaelp = new JMenu("Hjaelp");
		menuBar.add(mnHaelp);
		
		JMenuItem mntmAbout = new JMenuItem("Om");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAbout();
			}
		});
		mnHaelp.add(mntmAbout);
	}
	
	private void showAbout() {
        JOptionPane.showMessageDialog(frmVestbjergByggecenterAs, 
                    "DM78 1. Semester projekt  \n\n" + 
                    "Brian Lassen\n" +
                    "Christian Hansen Nørgaard\n" +
                    "Poul-Erik Christiansen\n" + 
                    "Stephan Tinpair Jessen\n" + 
                    "Tobias Dejbjerg\n" +
                    "\nVersion 1.0",
                    "Vestbjerg Byggecenter A/S", 
                    JOptionPane.INFORMATION_MESSAGE);
    }
	
	private void loadFromFile()	{
		try {
			PC.loadFromFile();
			IC.loadFromFile();
			OC.loadFromFile();
        }
        catch (Exception ex) {
            JOptionPane.showMessageDialog(frmVestbjergByggecenterAs, 
                    ex + "\n" + JOptionPane.WARNING_MESSAGE);
        }
	}
	   
	private void saveToFile() {
		try {
			PC.saveToFile();
			IC.saveToFile();
			OC.saveToFile();
        }
        catch (Exception ex) {
        	JOptionPane.showMessageDialog(frmVestbjergByggecenterAs, 
                    ex + "\n" + JOptionPane.WARNING_MESSAGE);
        }
	}

}
